package com.lagou.task10.test;

import java.util.Scanner;

/**
 * 编程实现控制台版并支持两人对战的五子棋游戏。
 * （1）绘制棋盘 - 写一个成员方法实现
 * （2）提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。
 * （3）每当一方下棋后判断是否获胜 - 写一个成员方法实现。
 * （4）提示： 采用二维数组来模拟并描述棋盘，棋盘如下：
 */
public class FiveChessGame {

    public static void main(String[] args) {
        //绘制棋盘
        char[][] arr = getInstance();
        //提示双方下棋,并判断胜负后退出
        playChess(arr);

    }


    private static  char[][] arr = new char[17][17] ;

    //3. 初始化五子棋盘
    public static char[][] getInstance(){
        //声明二位数组
        char[][] arr = new char[17][17];
        //对二维数组赋值
        for(int i = 0;i<arr.length;i++){
            for(int j = 0;j<arr[i].length;j++){
                if(i==0&&j==0){
                    arr[i][j]= 32  ;
                }else if(i*j==0){
                    char ch= 32;
                    int num = i==0?j:i;
                    if(num>10){
                        ch = (char)(num+86);
                    }else{
                        ch = (char)(num+47);
                    }
                    arr[i][j]= ch  ;
                }else{
                    arr[i][j]= '+' ;
                }
            }
        }
        showDrawingBoard(arr);
        return arr;
    }


    //提示双方下棋
    private static char[][] playChess(char[][] arr ){
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        String str ="";
        int player = 1;
        int x = -1;//定义下标
        int y = -1;//定义下标
        char ch = 'O';
        do{
            if (player == 1) {
                System.out.println("白棋输入下标：");
            }else{
                System.out.println("黑棋输入下标：");
            }
            if(sc.hasNextLine()){
                str = sc.nextLine();
               //判断字符串是否合法
                if (isLegal(str,arr)) {
                    String[] subscript = str.trim().split(" ");
                    x = Integer.parseInt(subscript[0]);
                    y = Integer.parseInt(subscript[1]);
                    //重新绘制棋盘
                    if (player == 1) {
                        ch = 'O';
                        arr = drawingBoard( x, y,arr, ch);
                        player = -1;
                    }else{
                        ch = 'X';
                        arr = drawingBoard( x, y,arr, ch);
                        player = 1;
                    }
                }
            }

            System.out.println("--------------------------------");
            //判断是否已连成五子棋
            if (x != -1 && y!= -1) {
                if (checkSuccess(x, y,arr, ch)) {
                    flag = false;
                }
            }
        }while (flag);

        if (player == -1) {
            System.out.println("白棋胜！");
        }else{
            System.out.println("黑棋胜！");
        }

        return arr;
    }


    /**
     * 判断输入内容是否合法
     * @param str
     * @return
     */
    public static boolean isLegal(String str,char[][] arr){
        if (str == null || str.trim().equals("")) {
            return false;
        }

        str = str.trim();
        String[] arr1 = str.split(" ");
        if (arr1.length!=2 ) {
            return false;
        }

        for (int j = 0; j < arr1.length; j++) {
            String str1 = arr1[j];
            for(int i =0;i< str1.length();i++){
                int chr = str1.charAt(i);
                if(chr < 48||chr > 57){
                    return false;
                }
            }
        }

        String[] subscript = str.split(" ");
        int x = Integer.parseInt(subscript[0]);
        int y = Integer.parseInt(subscript[1]);

        if (x < 1 || x >16 || y < 1 || y >16) {
            System.out.println("下标越界！");
            return false;
        }

        if (arr[x][y] != '+') {
            System.out.println("该坐标已存在值，请重新输入");
            return false;
        }

        return true;
    }


    /**
     * 判断输赢
     * @param x 横坐标
     * @param y 纵坐标
     * @param arr 变动的棋盘
     * @param ch  变动的字符
     * @return
     */
    public static boolean checkSuccess(int x, int y,char[][] arr,char ch){

        //判断横坐标是否五子连珠
        int count1 = checkYEquals(x, y,arr, ch);

        //判断纵坐标是否五子连珠
        int count2 = checkYEquals( x, y,arr, ch);
        //判断左上右下是否五子连珠
        int count3 = checkTopLeftEquals( x, y,arr, ch);
        //判断左下右上是否五子连珠
        int count4 = checkLowerLeftEquals( x, y,arr, ch);
        if (count1 == 5||count2 == 5||count3 == 5||count4 == 4) {
            return true;
        }

        System.out.println("count1-->"+count1);
        System.out.println("count2-->"+count2);
        System.out.println("count3-->"+count3);
        System.out.println("count4-->"+count4);
        return false;
    }


    /**
     * 判断横向是否相等
     * @param x
     * @param y
     * @param arr
     * @param ch
     * @return
     */
    public static  int  checkXEquals(int x, int y,char[][] arr,char ch){
        int count = 1;
        if (checkEquals(x, y-1,arr, ch)){
            count++;
            if (checkEquals(x, y-2,arr, ch)){
                count++;
                if (checkEquals(x, y-3,arr, ch)){
                    count++;
                    if (checkEquals(x, y-4,arr, ch)){
                        count++;
                    }
                }
            }
        }

        if (checkEquals(x, y+1,arr, ch)){
            count++;
            if (checkEquals(x, y+2,arr, ch)){
                count++;
                if (checkEquals(x, y+3,arr, ch)){
                    count++;
                    if (checkEquals(x, y+4,arr, ch)){
                        count++;
                    }
                }
            }
        }
        return count;
    }

    /**
     * 判断纵向是否相等
     * @param x
     * @param y
     * @param arr
     * @param ch
     * @return
     */
    public static  int  checkYEquals(int x, int y,char[][] arr,char ch){
        int count = 1;
        if (checkEquals(x-1, y,arr, ch)){
            count++;
            if (checkEquals(x-2, y,arr, ch)){
                count++;
                if (checkEquals(x-3, y,arr, ch)){
                    count++;
                    if (checkEquals(x-4, y,arr, ch)){
                        count++;
                    }
                }
            }
        }

        if (checkEquals(x+1, y,arr, ch)){
            count++;
            if (checkEquals(x+2, y,arr, ch)){
                count++;
                if (checkEquals(x+3, y,arr, ch)){
                    count++;
                    if (checkEquals(x+4, y,arr, ch)){
                        count++;
                    }
                }
            }
        }
        return count;
    }


    /**
     * 判断左上右下是否相等
     * @param x
     * @param y
     * @param arr
     * @param ch
     * @return
     */
    public static  int  checkTopLeftEquals(int x, int y,char[][] arr,char ch){
        int count = 1;
        if (checkEquals(x-1, y-1,arr, ch)){
            count++;
            if (checkEquals(x-2, y-2,arr, ch)){
                count++;
                if (checkEquals(x-3, y-3,arr, ch)){
                    count++;
                    if (checkEquals(x-4, y-4,arr, ch)){
                        count++;
                    }
                }
            }
        }

        if (checkEquals(x+1, y+1,arr, ch)){
            count++;
            if (checkEquals(x+2, y+2,arr, ch)){
                count++;
                if (checkEquals(x+3, y+3,arr, ch)){
                    count++;
                    if (checkEquals(x+4, y+4,arr, ch)){
                        count++;
                    }
                }
            }
        }
        return count;
    }


    /**
     * 判断左下右上是否相等
     * @param x
     * @param y
     * @param arr
     * @param ch
     * @return
     */
    public static  int  checkLowerLeftEquals(int x, int y,char[][] arr,char ch){
        int count = 1;
        if (checkEquals(x+1, y-1,arr, ch)){
            count++;
            if (checkEquals(x+2, y-2,arr, ch)){
                count++;
                if (checkEquals(x+3, y-3,arr, ch)){
                    count++;
                    if (checkEquals(x+4, y-4,arr, ch)){
                        count++;
                    }
                }
            }
        }

        if (checkEquals(x-1, y+1,arr, ch)){
            count++;
            if (checkEquals(x-2, y+2,arr, ch)){
                count++;
                if (checkEquals(x-3, y+3,arr, ch)){
                    count++;
                    if (checkEquals(x-4, y+4,arr, ch)){
                        count++;
                    }
                }
            }
        }
        return count;
    }


    /**
     *  判断坐标中的值是否相等
     *  x y 不能下标越界
     * @param x
     * @param y
     * @param arr
     * @param ch
     * @return
     */
    public static boolean  checkEquals(int x, int y,char[][] arr,char ch){
        if (x < 1 || x >16 ) {
            return false;
        }
        if (y < 1 || y> 16) {
            return false;
        }
        return arr[x][y] == ch;
    }



    //重新绘制棋盘
    private static char[][] drawingBoard(int x, int y,char[][] arr,char ch){
         arr[x][y]= ch;
         showDrawingBoard(arr);
         return arr;
    }

    /**
     * 棋盘展示
     * @param arr
     */
    public static void showDrawingBoard(char[][] arr ){

        //打印数组
        for(int i = 0;i<arr.length;i++){
            for(int j = 0;j<arr[i].length;j++){
                System.out.print(" "+arr[i][j]);
            }
            System.out.println();
        }

    }



}
